package com.geekchique.runningbuddy.controller;

import java.util.ArrayList;
import java.util.List;

import android.graphics.BitmapFactory;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import android.view.KeyEvent;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.activity.MapOfJog;
import com.geekchique.runningbuddy.util.DBHelperImpl;
import com.geekchique.runningbuddy.util.IDBHelper;
import com.geekchique.runningbuddy.view.MapOfJogView;
import com.geekchique.runningbuddy.view.mapofjog.JogRouteImpl;
import com.geekchique.runningbuddy.view.mapofjog.MapOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayController;
import com.google.android.maps.Point;

public class MapOfJogControllerImpl implements IMapOfJogController{

	public MapOfJogView mapOfJogView;
	private MapController mapViewController;
	private MapOfJog mapOfJogActivity;
	private Spinner pointCollectionList;
	private OverlayController overlayController;
	public Location currentGPSLocationOfPhone = null;
	protected LocationManager locationManager = null;
	protected IDBHelper dbHelper;
	final Handler mHandler;
	
    protected List<Location> path = new ArrayList<Location>();
	
	private String[][] locations = 	{ 
			  { "Tullis", "-4.229920, 55.846809" },
	          { "Glasgow Green", "-4.232676,55.846904" }
		};
	private MapOverlay googleMapOverlay;
	
	public MapOfJogControllerImpl(MapOfJog mapOfJog) {
		this.mapOfJogActivity = mapOfJog;
		this.mHandler = new Handler();
		this.dbHelper = new DBHelperImpl();
	}

	public void createMapOfJogView(Button goButton, Spinner pointCollection, int spinnerDropDownItems, MapView mapView) {
		this.mapOfJogView = new MapOfJogView(mapOfJogActivity, this, goButton, mapView);
		this.pointCollectionList = pointCollection;
		this.mapViewController = mapView.getController();
		
//		locations = dbHelper.getTodaysLastJogRoute();
		setSpinnerPresenter(populateSpinnerListPresenter(mapOfJogActivity, spinnerDropDownItems, locations));
		
		this.overlayController = mapView.createOverlayController();
		
		this.googleMapOverlay = 
			new MapOverlay(
					this.mapOfJogActivity,
					BitmapFactory.decodeResource(this.mapOfJogActivity.getResources(), R.drawable.mappin_blue),
					BitmapFactory.decodeResource(this.mapOfJogActivity.getResources(), R.drawable.mappin_red),
					BitmapFactory.decodeResource(this.mapOfJogActivity.getResources(), R.drawable.lower_left_info)
			);
		
		overlayController.add(googleMapOverlay, true);
		
	}

	private ArrayAdapter<CharSequence> populateSpinnerListPresenter(MapActivity mapOfJogActivity, int spinnerDropDownListID, String[][] locations) {
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>( mapOfJogActivity, spinnerDropDownListID);
		
		for (int i = 0; i < locations.length; i++){
			adapter.addObject(locations[i][0]);
		}
		
		adapter.setDropDownViewResource(spinnerDropDownListID);
		return adapter;
	}
	
	private void setSpinnerPresenter(ArrayAdapter<CharSequence> adapter) {
	      this.pointCollectionList.setAdapter(adapter);
	}


	public void gotoCurrentGPSPosition() {
	      
		Point point = new Point((int) (this.currentGPSLocationOfPhone.getLatitude() * 1000000), (int) (this.currentGPSLocationOfPhone.getLongitude() * 1000000));
	      
		mapViewController.animateTo(point);
		mapViewController.zoomTo(18);
		mapOfJogView.invalidateMapView();

	}
	
//	private Point getMapPointFromLocationList(int pos) {
//		String[] loc = locations[pos][1].split(",");
//	      
//		double lat = Double.parseDouble(loc[1]);
//		double lon = Double.parseDouble(loc[0]);
//	      
//		return new Point((int) (lat * 1000000), (int) (lon * 1000000));
//	}

	public void zoomTo(int zoomLevel) {
		this.mapViewController.zoomTo(zoomLevel);
	}

	public JogRouteImpl getCurrentJogRoute() {
		return new JogRouteImpl();
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
			
			switch (keyCode) {
				case KeyEvent.KEYCODE_I:
						mapViewController.zoomTo(mapOfJogView.getZoomLevel() + 1);
						break;
				case KeyEvent.KEYCODE_O:
						mapViewController.zoomTo(mapOfJogView.getZoomLevel() - 1);
						break;
				case KeyEvent.KEYCODE_V:
					draw1Point();
					break;
				case KeyEvent.KEYCODE_B:
					draw2Point();
					break;
				case KeyEvent.KEYCODE_N:
					draw3Point();
					break;
				case KeyEvent.KEYCODE_M:
					draw4Point();
					break;
				case KeyEvent.KEYCODE_L:
					drawPoints();
					break;
				case KeyEvent.KEYCODE_S:
					mapOfJogView.toggleSatellite();
					break;
		      }
			
		      return true;
	}
	
	public void draw1Point() {
		addPoint(55846809, -4229920);
	}
	
	public void draw2Point() {
		addPoint(55846809, -4231200);
	}
	
	public void draw3Point() {
		addPoint(55851200, -4236400);
	}
	
	public void draw4Point() {
		addPoint(55851000, -4236800);
	}
	
	
    private boolean addPoint(double latitude, double longitude) {
    	Location pointLocation = new Location();
    	pointLocation.setLatitude(latitude);
    	pointLocation.setLongitude(longitude);
    	this.googleMapOverlay.addLocationToPath(pointLocation);
		mapOfJogView.invalidateMapView();
    	return true;
    }

	public void drawPoints() {
		draw1Point();
		draw2Point();
		draw3Point();
		draw4Point();
	}

	public void updateRunningPath() {
		if(this.currentGPSLocationOfPhone != null){
	    	Location pointLocation = new Location();
	    	pointLocation.setLatitude(this.currentGPSLocationOfPhone.getLatitude() * 1000000);
	    	pointLocation.setLongitude(this.currentGPSLocationOfPhone.getLongitude() * 1000000);
	    	this.googleMapOverlay.addLocationToPath(pointLocation);
			gotoCurrentGPSPosition();
		}
	}

	public void setCurrentGPSLocationOfPhone(Location currentLocation) {
		this.currentGPSLocationOfPhone = currentLocation;
	}

}
